package jp.bsws.action.relation;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.KeyValueBean;
import jp.bsws.bean.TenpoRelationBean;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.BSWSDao;
import jp.bsws.dao.GenkinShutsunoDao;
import jp.bsws.dao.HosyuShubetsuDao;
import jp.bsws.dao.JugyoinDao;
import jp.bsws.dao.RaitenYoinDao;
import jp.bsws.dao.ShiharaiHohoDao;
import jp.bsws.dao.SyohizeiDao;
import jp.bsws.dao.TenpoRelationDao;
import jp.bsws.dao.UriageShubetsuDao;
import jp.bsws.model.TenpoRelationModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/*
 * メインメニューアクションクラス
 */
public class TenpoRelationAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	private TenpoRelationBean tenpoRelationBean;
	private List<KeyValueBean> sansyoTenpoList;
	private List<KeyValueBean> sansyoRaitenYoinList;
	private List<KeyValueBean> sansyoUriageShubetsuList;
	private List<KeyValueBean> sansyoShiharaiHohoList;
	private List<KeyValueBean> sansyoHosyuShubetsuList;
	private List<KeyValueBean> sansyoGenkinShutsunoList;
	private List<KeyValueBean> sansyoSyohizeiList;
	private List<KeyValueBean> sansyoJugyoinList;

	private String strSansyoTenpoCod;

	public void validateUpdateTenpo() {

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

	}

	private void init() throws Exception{
		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		//店舗関係情報が無ければ、一つ作る。
		if( this.tenpoRelationBean == null){
			this.tenpoRelationBean = new TenpoRelationBean();
		}
		if( BSWSUtils.isBlankNull(this.strSansyoTenpoCod)){
			this.strSansyoTenpoCod = bSWSSession.getStrTenpoCod();
		}
		Connection con = DBUtils.getConnection();
		try{
			//画面表示内容の設定
			hyojiJunbi(con);
		}finally{
			con.close();
		}
	}

	@Override
	public String execute() throws Exception{

		//初期設定
		init();
		Connection con = DBUtils.getConnection();
		try{
			//店舗関連情報
			setTenpoRelation(con);
		}finally{
			con.close();
		}
		return SUCCESS;
	}

	/**
	 * 来店要因追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String addRY() throws Exception{
		//初期設定
		init();
		List<String> tmpList = tenpoRelationBean.getRaitenYoinList();
		tmpList.add("-1");
		tenpoRelationBean.setRaitenYoinList(tmpList);
		return SUCCESS;
	}

	/**
	 * 売上種別追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String addUS() throws Exception{
		//初期設定
		init();
		List<String> tmpList = tenpoRelationBean.getUriageShubetsuList();
		tmpList.add("-1");
		tenpoRelationBean.setUriageShubetsuList(tmpList);
		return SUCCESS;
	}

	/**
	 * 支払方法追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String addSH() throws Exception{
		//初期設定
		init();
		List<String> tmpList = tenpoRelationBean.getShiharaiHohoList();
		tmpList.add("-1");
		tenpoRelationBean.setShiharaiHohoList(tmpList);
		return SUCCESS;
	}

	/**
	 * 報酬種別追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String addHS() throws Exception{
		//初期設定
		init();
		List<String> tmpList = tenpoRelationBean.getHosyuShubetsuList();
		tmpList.add("-1");
		tenpoRelationBean.setHosyuShubetsuList(tmpList);
		return SUCCESS;
	}

	/**
	 * 現金出納追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String addGS() throws Exception{
		//初期設定
		init();
		List<String> tmpList = tenpoRelationBean.getGenkinShutsunoList();
		tmpList.add("-1");
		tenpoRelationBean.setGenkinShutsunoList(tmpList);
		return SUCCESS;
	}

	/**
	 * 消費税追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String addSZ() throws Exception{
		//初期設定
		init();
		List<String> tmpList = tenpoRelationBean.getSyohizeiList();
		tmpList.add("-1");
		tenpoRelationBean.setSyohizeiList(tmpList);
		return SUCCESS;
	}

	/**
	 * 従業員追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String addJG() throws Exception{
		//初期設定
		init();
		List<String> tmpList = tenpoRelationBean.getJugyoinList();
		tmpList.add("-1");
		tenpoRelationBean.setJugyoinList(tmpList);
		return SUCCESS;
	}

	public String updateTenpoRelation() throws Exception{

		//初期設定
		init();

		TenpoRelationDao tenpoRelationDao = TenpoRelationDao.getInstance();

		Connection con = DBUtils.getConnection();
		try{
			// 店舗の情報全部削除
			tenpoRelationDao.deleteTenpoRelation(con, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
			// 店舗の情報全部登録
			tenpoRelationDao.insertTenpoRelationAll(con, bSWSSession, strSansyoTenpoCod, tenpoRelationBean);

			con.commit();

			// 画面出力内容取得
			setTenpoRelation(con);

		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws Exception{
		RaitenYoinDao raitenYoinDao = RaitenYoinDao.getInstance();
		UriageShubetsuDao uriageShubetsuDao = UriageShubetsuDao.getInstance();
		ShiharaiHohoDao shiharaiHohoDao = ShiharaiHohoDao.getInstance();
		HosyuShubetsuDao hosyuShubetsuDao = HosyuShubetsuDao.getInstance();
		GenkinShutsunoDao genkinShutsunoDao = GenkinShutsunoDao.getInstance();
		SyohizeiDao syohizeiDao = SyohizeiDao.getInstance();
		JugyoinDao jugyoinDao = JugyoinDao.getInstance();

		// KeyValue情報取得
		this.sansyoTenpoList = bSWSSession.getSansyoTenpoList();
		this.sansyoRaitenYoinList = raitenYoinDao.getRaitenYoinList(con, bSWSSession.getStrGroupCod(), null);
		this.sansyoUriageShubetsuList = uriageShubetsuDao.getUriageShubetsuList(con, bSWSSession.getStrGroupCod(), null);
		this.sansyoShiharaiHohoList = shiharaiHohoDao.getShiharaiHohoList(con, bSWSSession.getStrGroupCod(), null);
		this.sansyoHosyuShubetsuList = hosyuShubetsuDao.getHosyuShubetsuList(con, bSWSSession.getStrGroupCod(), null);
		this.sansyoGenkinShutsunoList = genkinShutsunoDao.getGenkinShubetsuList(con, bSWSSession.getStrGroupCod(), null);
		this.sansyoSyohizeiList = syohizeiDao.getSyohizeiList(con, bSWSSession.getStrGroupCod(), null);
		this.sansyoJugyoinList = jugyoinDao.getJugyoinList(con, bSWSSession.getStrGroupCod(), null);
	}

	private void setTenpoRelation(Connection con) throws Exception{
		TenpoRelationDao tenpoRelationDao = TenpoRelationDao.getInstance();

		// 関連情報取得
		List<TenpoRelationModel> tenpoRelationList = tenpoRelationDao.getTenpoRelationInfo(con, bSWSSession.getStrGroupCod(), strSansyoTenpoCod, null);
		List<String> tmpRYList = new ArrayList<String>();
		List<String> tmpUSList = new ArrayList<String>();
		List<String> tmpSHList = new ArrayList<String>();
		List<String> tmpHSList = new ArrayList<String>();
		List<String> tmpGSList = new ArrayList<String>();
		List<String> tmpSZList = new ArrayList<String>();
		List<String> tmpJGList = new ArrayList<String>();
		for(int i = 0; i < tenpoRelationList.size(); i++){
			TenpoRelationModel tmpTenpoRelationModel = tenpoRelationList.get(i);
			String strSeq = String.valueOf(tmpTenpoRelationModel.getIntSeq());
			switch (tmpTenpoRelationModel.getIntRelationKbn()) {
			case BSWSConstants.KBN_RAITEN_YOIN:
				tmpRYList.add(strSeq);
				break;
			case BSWSConstants.KBN_URIAGE_SHUBETSU:
				tmpUSList.add(strSeq);
				break;
			case BSWSConstants.KBN_SHIHARAI_HOHO:
				tmpSHList.add(strSeq);
				break;
			case BSWSConstants.KBN_HOSYU_SHUBETSU:
				tmpHSList.add(strSeq);
				break;
			case BSWSConstants.KBN_GENKIN_SHUTSUNO:
				tmpGSList.add(strSeq);
				break;
			case BSWSConstants.KBN_SYOHIZEI:
				tmpSZList.add(strSeq);
				break;
			case BSWSConstants.KBN_JUGYOIN:
				tmpJGList.add(strSeq);
				break;
			default:
				break;
			}
		}

		if (tmpRYList.size() == 0){tmpRYList.add("-1");}
		if (tmpUSList.size() == 0){tmpUSList.add("-1");}
		if (tmpSHList.size() == 0){tmpSHList.add("-1");}
		if (tmpHSList.size() == 0){tmpHSList.add("-1");}
		if (tmpGSList.size() == 0){tmpGSList.add("-1");}
		if (tmpSZList.size() == 0){tmpSZList.add("-1");}
		if (tmpJGList.size() == 0){tmpJGList.add("-1");}

		this.tenpoRelationBean.setRaitenYoinList(tmpRYList);
		this.tenpoRelationBean.setUriageShubetsuList(tmpUSList);
		this.tenpoRelationBean.setShiharaiHohoList(tmpSHList);
		this.tenpoRelationBean.setHosyuShubetsuList(tmpHSList);
		this.tenpoRelationBean.setGenkinShutsunoList(tmpGSList);
		this.tenpoRelationBean.setSyohizeiList(tmpSZList);
		this.tenpoRelationBean.setJugyoinList(tmpJGList);
	}

	public BSWSSession getBSWSSession() {
		return bSWSSession;
	}

	public void setBSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public List<KeyValueBean> getSansyoTenpoList() {
		return sansyoTenpoList;
	}

	public void setSansyoTenpoList(List<KeyValueBean> sansyoTenpoList) {
		this.sansyoTenpoList = sansyoTenpoList;
	}

	public List<KeyValueBean> getSansyoRaitenYoinList() {
		return sansyoRaitenYoinList;
	}

	public void setSansyoRaitenYoinList(List<KeyValueBean> sansyoRaitenYoinList) {
		this.sansyoRaitenYoinList = sansyoRaitenYoinList;
	}

	public List<KeyValueBean> getSansyoUriageShubetsuList() {
		return sansyoUriageShubetsuList;
	}

	public void setSansyoUriageShubetsuList(
			List<KeyValueBean> sansyoUriageShubetsuList) {
		this.sansyoUriageShubetsuList = sansyoUriageShubetsuList;
	}

	public List<KeyValueBean> getSansyoShiharaiHohoList() {
		return sansyoShiharaiHohoList;
	}

	public void setSansyoShiharaiHohoList(List<KeyValueBean> sansyoShiharaiHohoList) {
		this.sansyoShiharaiHohoList = sansyoShiharaiHohoList;
	}

	public List<KeyValueBean> getSansyoHosyuShubetsuList() {
		return sansyoHosyuShubetsuList;
	}

	public void setSansyoHosyuShubetsuList(
			List<KeyValueBean> sansyoHosyuShubetsuList) {
		this.sansyoHosyuShubetsuList = sansyoHosyuShubetsuList;
	}

	public List<KeyValueBean> getSansyoGenkinShutsunoList() {
		return sansyoGenkinShutsunoList;
	}

	public void setSansyoGenkinShutsunoList(
			List<KeyValueBean> sansyoGenkinShutsunoList) {
		this.sansyoGenkinShutsunoList = sansyoGenkinShutsunoList;
	}

	public List<KeyValueBean> getSansyoSyohizeiList() {
		return sansyoSyohizeiList;
	}

	public void setSansyoSyohizeiList(List<KeyValueBean> sansyoSyohizeiList) {
		this.sansyoSyohizeiList = sansyoSyohizeiList;
	}

	public List<KeyValueBean> getSansyoJugyoinList() {
		return sansyoJugyoinList;
	}

	public void setSansyoJugyoinList(List<KeyValueBean> sansyoJugyoinList) {
		this.sansyoJugyoinList = sansyoJugyoinList;
	}

	public String getStrSansyoTenpoCod() {
		return strSansyoTenpoCod;
	}

	public void setStrSansyoTenpoCod(String strSansyoTenpoCod) {
		this.strSansyoTenpoCod = strSansyoTenpoCod;
	}

	public TenpoRelationBean getTenpoRelationBean() {
		return tenpoRelationBean;
	}

	public void setTenpoRelationBean(TenpoRelationBean tenpoRelationBean) {
		this.tenpoRelationBean = tenpoRelationBean;
	}

}